<template>
	<div ref="mgrDialog" class="dialog-wrap">
		  
		<el-dialog 
			:title="headerTitle" 
			width="40%"
			@close="resetDialog" 
			:visible.sync="DialogVisible"
		>
			
			<el-row :gutter="20">

				<el-form 
					v-if = "editForm != null"
					ref="dialogForm"
					:rules="rules"
					size="mini"
					:label-width="formLabelWidth"
					:model.sync="editForm">
					
					<el-col :span="22">
						<el-form-item label="名称" prop="name">
		      				<el-input v-model="editForm.name" autocomplete="off" placeholder="请输入设备名称"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col :span="22">	
		    			<el-form-item label="父分组" prop="parentId">

		    				<input-tree
                  				class="width-full"
                  				v-model="editForm.parentId"
                  				:name="editForm.pName"
                  				:data="groupTree"
                  				:props="equipGroupProps"
                  				node-key="id"
                  				node-name="name"
                  				@selected="groupSelect"
                			></input-tree>

		    			</el-form-item>
		    		</el-col>

		    		
		    			
				</el-form>
			</el-row>
			
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" type="primary" @click="confirm">确定</el-button>
			    <el-button size="mini" type="danger" @click="cancel">取消</el-button>
			</div>	
		</el-dialog>

	</div>
</template>


<script>
import { list as groupList } from "@/api/equipment/group"
import { getRolesTree,getRoleTreeList } from "@/api/admin/roles"
import { getUserInfo } from "@/api/user"
import { convertDataToTree } from "@/utils/util"
import InputTree from "@/components/InputTree"
export default{
	props:{
		//新增还是编辑
	    type:{
	      type:[Number,String],
	      defalut:1
	    },
		visible:{
			type:Boolean,
			default:false
		},
		form:{
			type:Object,
			default:null
		}
	},
	components:{
    	InputTree
  	},
	watch:{
		form(newVal,oldVal){
			if(newVal != null){
				this.editForm = newVal;
			}else{
				this.editForm = this.defaultForm;
			}	
		},
		type(newVal,oldVal){
	      	if(newVal == 1){
	        	this.headerTitle = "添加设备"
	        	this.editForm = this.defaultForm;
	      	}else if(newVal == 2){
	        	this.headerTitle = "修改设备"
	      	}
	    },
	    visible(val){
	    	this.DialogVisible = val;
	    	if(this.form === null){
	    		this.editForm = this.defaultForm;
	    	}
	    }
	},
	data(){
		return{
			headerTitle:"添加设备",
			DialogVisible:false,
			defaultForm:{
				//设备名称
				name:'',
				pName:'根分组',
				parentId:-1
			},
			groupTree:[],
			testEquipmentGroup:[
				{
					checked: true, 
					iconSkin: null, 
					id: 0, 
					name: "根目录", 
					open: true, 
					pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 1, 
				 	name: "继电器设备", 
				 	open: true, 
				 	pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 2, 
				 	name: "采集设备", 
				 	open: true, 
				 	pId: 0
				}
			],
			equipGroupProps:{
        		children: 'children',
        		label: 'name',
        		value: 'id'
      		},
			editForm:null,
			formLabelWidth:'100px',
			//验证规则
		    rules:{
		        name:[{required:true,message:"请输入设备名称",trigger:"blur"}],
		        parentId:[{required:true,message:"请选择父分组",trigger:"blur"}],
		    },
		}
	},
	mounted(){
		this.getRoleTree();
	},
	methods:{
		//获取树形分组
		getRoleTree(){

			groupList().then((res)=>{
				console.log("-----------获取树形菜单-----------")
				console.log(res);
				const result = res.data.data.records;
				//this.roleTree = convertDataToTree(res.data);
				this.groupTree = this.convertGroupList(result)
			})

		},
		convertGroupList(data){
			if(!data.length){
				return [{id:-1,name:'根分组',children:[]}]
			}

			if(data[0].id !== -1){
				return [{id:-1,name:'根分组',children:data}]
			}

			return data;
		},
		resetDialog(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		//上级角色选择
		groupSelect(data){
			this.editForm.parentId = data.id;
			this.editForm.pName = data.name;
		},
		//取消按钮
		cancel(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		confirm(){
			this.$refs.dialogForm.validate((valid)=>{
		        if (valid) {
		          this.DialogVisible = false;

		          this.$emit('confirm',{type:this.type,data:this.editForm});

		        } else {
		           
		            return false;
		        }

      		})
		}

	}
}


</script>

<style scoped>
	.with-full{
	    width:100%!important;
	}
</style>